﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SFML.Graphics;
using SFML.Window;
using Strom.Core.Maps;
using Strom.Core.World;

namespace Strom.Client.Graphic
{
    public class GameLoop
    {
        public static void Start()
        {
            // TEMP
            MapDatas temp = new MapDatas();
            Var.GameWorld.Maps.Add(temp);

            while (Var.Loaded)
            {
                Var.RenderWin.Clear(Color.Black);



                DrawLayers();
                DrawStats(ClientManager.Client.Client.CurrentCharacter);
                
                Var.RenderWin.Display();
                Application.DoEvents();
            }
        }

        static void DrawStats(Core.Characters.Character character)
        {
            Sprite background = new Sprite(new Texture(new Image(80, 60, new Color(0, 0, 0, 100))))
                                    {Position = new Vector2f(0, 0)};
            Var.RenderWin.Draw(background);
            TextEngine.Draw(string.Format("Niveau: {0}\nVie: {1}\nMagie: {2}",character.Stats.Level.ToString(), character.Stats.HP.ToString(), character.Stats.MP.ToString()), 0, 0, Color.Cyan, false, Color.Blue);
        }

        static void DrawCharacter()
        {
            byte orr = ClientManager.Client.Client.CurrentCharacter.Graphics.Orientation;
            Var.RenderWin.Draw(
                new Sprite(
                    new Texture(
                        ClientManager.Client.Client.CurrentCharacter.Graphics.Sprite.
                            GetAnimMemStreamFromCurrentOrientation(orr)[0]))
                    {Position = new Vector2f(ClientManager.GUI.Game.Width/2, ClientManager.GUI.Game.Height/2)});
        }

        static void DrawLayers()
        {
            for (byte x = 0; x <= 25; x++)
            {
                for (byte y = 0; y <= 17; y++)
                {
                    // Affichage des couches inférieures
                    if (Var.GameWorld.Maps[0].Cells[x, y].Sol.TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Sol.TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Sol.TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 0);
                    }
                    if (Var.GameWorld.Maps[0].Cells[x, y].Inf[0].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Sol.TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Sol.TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 1);
                    }
                    if (Var.GameWorld.Maps[0].Cells[x, y].Inf[1].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Inf[1].TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Inf[1].TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 2);
                    }
                    if (Var.GameWorld.Maps[0].Cells[x, y].Inf[2].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Inf[2].TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Inf[2].TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 3);
                    }
                }
            }

            for (byte x = 0; x <= 25; x++)
            {
                for (byte y = 0; y <= 17; y++)
                {
                    // Affichage des couches supérieurs
                    if (Var.GameWorld.Maps[0].Cells[x, y].Sup[0].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Sup[0].TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Sup[0].TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 4);
                    }

                    if (Var.GameWorld.Maps[0].Cells[x, y].Sup[1].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Sup[1].TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Sup[1].TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 5);
                    }

                    if (Var.GameWorld.Maps[0].Cells[x, y].Sup[2].TilesNum != "" & !(Var.GameWorld.Maps[0].Cells[x, y].Sup[2].TilesX == 0 & Var.GameWorld.Maps[0].Cells[x, y].Sup[2].TilesY == 0))
                    {
                        Functions.DrawTiles(0, x, y, 6);
                    }
                }
            }
        }
    }
}
